import type Typings from "../../typings";

class ResController {
  constructor() {}

  // 响应成功
  Success: (
    res: Typings.Express.Response,
    data?: Typings.Common.plainObj | null,
    msg?: string
  ) => Typings.Express.Response = (res, data = null, msg = "响应成功") => {
    return res.send({
      code: 0,
      data,
      msg,
    });
  };

  // 请求参数出错
  paramsWrong: (
    res: Typings.Express.Response,
    msg?: string
  ) => Typings.Express.Response = (res, msg = "请求参数出错") => {
    return res.send({
      code: 500,
      data: null,
      msg,
    });
  };

  // 查询数据不存在
  isNotExist: (
    res: Typings.Express.Response,
    msg?: string
  ) => Typings.Express.Response = (res, msg = "查询数据不存在") => {
    return res.send({
      code: 500,
      data: null,
      msg,
    });
  };

  // 新增的数据已存在
  isExist: (
    res: Typings.Express.Response,
    msg?: string
  ) => Typings.Express.Response = (res, msg = "已存在该数据") => {
    return res.send({
      code: 500,
      data: null,
      msg,
    });
  };

  // 数据异常
  Abnormal: (
    res: Typings.Express.Response,
    msg?: string
  ) => Typings.Express.Response = (res, msg = "数据异常") => {
    return res.send({
      code: 500,
      data: null,
      msg,
    });
  };

  // 数据库异常
  Database: (
    res: Typings.Express.Response,
    err: unknown,
    msg?: string
  ) => Typings.Express.Response = (res, err, msg = "服务异常") => {
    console.log(err);
    return res.send({
      code: 502,
      data: err,
      msg,
    });
  };
}

export default ResController;
